{template 'web/hairsalon/base_menu'}
<script src="{MODULE_URL}template/resources/js/jquery-ui.min.js"></script>
<div class="top_tab">
    <a href="{php echo $this->createWebUrl('projects',array('op'=>'display'))}" class="layui-btn {if $operation!='display'}layui-btn-primary{/if}">项目管理</a>
    <a href="{php echo $this->createWebUrl('projects',array('op'=>'categorydisplay'))}" class="layui-btn {if $operation!='categorydisplay'}layui-btn-primary{/if}">分类管理</a>
</div>
<style>
    .panelbox-head i{ color:#009688;}
    .panelbox-head a{ margin-right:10px}
</style>

{if $operation == 'categorydisplay'}
<div class="panelbox">
    <div class="panelbox-head"> <A href="#" onclick="editBox(0)"><i class="fa fa-plus"></i>新增分类</A> <span style="color:#999">*拖动改变排序</span></div>
    <div class="panelbox-body">
        <div class="layui-form">
            <table class="layui-table">
                <thead>
                <tr>
                    <td>序号</td>
                    <th>名称</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {loop $category $row}
                <tr rid="{$row['id']}">
                    <td><input type="hidden" name="displayorder" value="{$row['id']}" />
                        <span class="order">{$row['displayorder']}</span></td>
                    <td>{$row['title']}</td>
                    <td>{if $row['status']}<span class="label label-info">显示</span>{else}<span class="label label-default">不显示</span>{/if}</td>
                    <td class="text-right"><a href="javascript:editBox({$row['id']})" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a> <a href="javascript:deletepcate('{php echo $this->createWebUrl('projects',array('op'=>'catedelete','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
                </tr>
                {/loop}
                </tbody>
            </table>
        </div>
    </div>
    <div class="panelbox-foot"> </div>
</div>

<div id="editcatebox" style="display:none">
    <form id="editcateform" class="layui-form" action="" style="padding:15px">
        <input type="hidden" name="id" />
        <div class="layui-form-item">
            <input type="text" name="title" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-item">
            <input type="checkbox" name="status" value="1" checked="checked" lay-skin="switch" lay-text="显示|禁用">
        </div>
    </form>
</div>
<script language="javascript">
    layui.use(['layer', 'laypage', 'element','form'], function(){
        var layer = layui.layer
                ,laypage = layui.laypage
                ,element = layui.element
                ,form = layui.form;
        form.render();
        form.on('submit(addcate)', function(data){

            return true;
        });
    });
    var fixHelper = function(e, ui) {
        ui.children().each(function() {
            $(this).width($(this).width());
        });
        return ui;
    };
    $(".layui-table tbody").sortable({
        helper: fixHelper,
        axis:"y",
        start:function(e, ui){
            ui.helper.css({"background":"#F60"});
        },
        stop:function(e, ui){
            ui.item.css({"background":"#FFF"});
            updateMove(e, ui);
        },
    }).disableSelection();

    function updateMove(){
        var temp={};
        $("input[name='displayorder']").each(function(index, element) {
            var val=$(this).val();
            temp[val]=index+1;
        });
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        $.post("{php echo $this->createWebUrl('projects',array('op'=>'changecateorder'))}",{displayorder:temp},function(returnstr){
            layer.close(index2);
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(result.success){
                layer.msg("更新排序成功",{icon:1,time:2000});
                $(".order").each(function(index, element) {
                    $(this).text(index+1);
                });
            }else{
                layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
            }
        });
    }

    function editBox(id){
        var index2=layer.load(1, {shade: [0.5,'#fff']});
        var til= id==0 ? "添加新分类":"修改分类";
        $("#editcateform")[0].reset();
        $.post("{php echo $this->createWebUrl('projects',array('op'=>'getcate'))}",{"id":id},function(data){
            layer.close(index2);
            if(!data.success){
                layer.msg(data.msg,{icon:1,time:2000});
                return;
            }
            $("#editcateform").find("input[name='id']").val(id);
            if(id==0){
                $("#editcateform input[name='status']").attr("checked",true);
            }else{
                var items=data.item;
                if(parseInt(items.status)==0){
                    $("#editcateform input[name='status']").attr("checked",false);
                }
                $("#editcateform").find("input[name='title']").val(items.title);
            }
            layer.open({
                type: 1,
                title:til,
                btn: ['确认', '关闭'],
                btn1:function(index,layero){
                    index2=layer.load(1, {shade: [0.5,'#fff']});
                    var param={"title":$("#editcateform").find("input[name='title']").val(),"status":$("#editcateform input[name='status']").is(":checked") ? 1 :0,"id":id}
                    $.post("{php echo $this->createWebUrl('projects',array('op'=>'categorypost'))}",param,function(data2){
                        layer.close(index2);
                        if(!data2.success){
                            layer.msg(data2.msg,{icon:1,time:2000});
                            return;
                        }
                        location.reload();
                    },'json');
                },
                content: $("#editcatebox").show(),
                end:function(){
                    $("#editcatebox").hide();
                }
            });
        },'json');

    }
    function deletepcate(url){
        layer.confirm('删除分类将同时删除该分类下的所有商品，且不可恢复', {}, function(){
            deleteContent(url);
        });
    }
</script>
{elseif $operation == 'display'}
<div class="panelbox">
    <form action="" method="post" class="layui-form" id="listform" enctype="multipart/form-data">
        <div class="panelbox-head form-inline">
            <A href="{php echo $this->createWebUrl('projects',array('op'=>'post'))}"><i class="fa fa-plus"></i> 新增</A>
            <div class="pull-right form-inline">
                <select id="pcatelist" style="display:inline-block;" lay-ignore onchange="jumptoNew()">
                    <option value="">全部分类</option>
                    {loop $cateAry $index $row}
                    <option value="{$index}" {if $index==$_GPC['pcate'] && $_GPC['pcate']!=''}selected{/if}>{$row}</option>
                    {/loop}
                </select>
            </div>
        </div>
        <div class="panelbox-body">
            <div class="layui-form">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th style="width:60px"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                        <th>名称</th>
                        <th>分类</th>
                        <th>价格</th>
                        <th>详细</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {loop $list $row}
                    <tr>
                        <td><input type="checkbox" name="goodid[]" value="{$row['id']}" lay-skin="primary"></td>
                        <td>{$row['title']}</td>
                        <td>{php echo $cateAry[$row['pcate']]}</td>
                        <td>{$row['price']}</td>
                        <td>
                            {php $commission=@explode(",",$row['commission']);$performance=@explode(",",$row['performance']);}
                            <?php for($i=0;$i<count($commission);$i++){?>
                            {php $tempary2=explode("#",$commission[$i]);}
                            {php $tempary1=explode("#",$performance[$i]);}
                            <div>
                                <b>{php echo $staffAry[$tempary1[0]]}</b>
                                业绩： <b>{php echo $tempary1[2]}</b>
                                {if $tempary1[1]==1}%{else}元{/if}
                                提成：
                                轮牌 <b>{php echo $tempary2[2]}</b>
                                {if $tempary2[1]==1}%{else}元{/if}
                                指定 <b>{php echo $tempary2[3]}</b> {if $tempary2[1]==1}%{else}元{/if}

                            </div>
                            <?php }?>
                        </td>
                        <td>{if $row['status']}<span class="label label-info">正常</span>{else}<span class="label label-default">下架</span>{/if}</td>
                        <td class="text-right">
                            <a href="{php echo $this->createWebUrl('projects',array('op'=>'post','id'=>$row['id']))}" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe642;</i></a>
                            <a href="javascript:deleteContent('{php echo $this->createWebUrl('projects',array('op'=>'delete','id'=>$row['id']))}')" class="layui-btn layui-btn-danger layui-btn-small"><i class="layui-icon">&#xe640;</i></a></td>
                    </tr>
                    {/loop}
                    </tbody>
                </table>
            </div>
        </div>
    </form>
</div>
{elseif $operation == 'post'}
<style>
    #ticheng input[type='text']{width: 80px;}
    #ticheng .title{font-weight: bolder;width: 80px;}
    #ticheng td{padding:5px;}
    #ticheng input[type='radio']{display:inline-block}
    #ticheng label{display: block;margin: 3px 0;}
</style>
<div class="panelbox">
    <form action="" method="post" onsubmit="return checkSubmit();" class="layui-form" enctype="multipart/form-data">
        <input type="hidden" name="id" value="{php echo $id}" />
        <div class="panelbox-head">{if !$id}添加{else}编辑{/if}</div>
        <div class="panelbox-body">
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="开启"  {if $item['status']}checked{/if} />
                    <input type="radio" name="status" value="0" title="禁用" {if !$item['status']}checked{/if} />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title"  class="layui-input" value="{$item['title']}" />
                    <div class="help-block"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属分类</label>
                <div class="layui-input-block">
                    <select name="pcate">
                        <option value="">所属分类</option>
                        {loop $category $row}
                        <option value="{$row['id']}" {if $row['id']==$item['pcate']}selected{/if}>{$row['title']}</option>
                        {/loop}
                    </select>
                    <div class="help-block"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">介绍</label>
                <div class="layui-input-block">
                    <textarea name="description" required placeholder="请输入" class="layui-textarea">{$item['description']}</textarea>
                    <div class="help-block">项目内容介绍</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">价格</label>
                <div class="layui-input-block form-inline">
                    <input type="text" name="price" lay-ignore  class="form-control" value="{$item['price']}" />
                    <div class="help-block"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所需工种</label>
                <div class="layui-input-block">
                    {loop $list $row}
                    <input type="checkbox" {if in_array($row['id'],$staffcateAry)}checked{/if} name="staffcate[]" lay-filter="staffcate" value="{$row['id']}" title="{$row['title']}">
                    {/loop}
                    <div class="help-block"></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">提成</label>
                <div class="layui-input-block">
                    <input type="hidden" name="commission" id="commission" value="{$item['commission']}" />
                    <input type="hidden" name="performance" id="performance" value="{$item['performance']}" />
                    <div id="ticheng" >

                    </div>
                    <div class="help-block"></div>
                </div>
            </div>

        </div>
        <div class="panelbox-foot">
            <input type="hidden" name="token" value="{$_W['token']}" />
            <input class="layui-btn" type="submit" name="submit" value="提交">
        </div>
    </form>
</div>
<script language="javascript">
    layui.use(['form'], function(){
        var form = layui.form;
        form.render();
        form.on('checkbox(staffcate)', function(data){
            var val=data.value;
            var title=$(data.elem).attr("title");
            insertContent(val,title);
        });
        strart();
    });
    function strart(){
        var tempary=("{$item['staffcate']}");
        if(tempary.length<1)return;
        tempary=tempary.split(',');
        for(var i in tempary){
            insertContent(tempary[i]);
        }
        var commission=("{$item['commission']}").split(",");
        var performance=("{$item['performance']}").split(",");
        for(var i=0;i<performance.length;i++){
            var temp=performance[i].split("#");
            $("input[name='projectitem"+temp[0]+"_t1'][value='"+temp[1]+"']").click();
            $("input[name='projectitem"+temp[0]+"_n"+temp[1]+"']").val(temp[2]);
            //
            console.log(commission[i]);
            var temp1=commission[i].split("#");
            $("input[name='projectitem"+temp1[0]+"_t2'][value='"+temp1[1]+"']").click();
            if(parseInt(temp1[1])==1){
                $("input[name='projectitem"+temp1[0]+"_nn1']").val(temp1[2]);
                $("input[name='projectitem"+temp1[0]+"_nn2']").val(temp1[3]);
            }else{
                $("input[name='projectitem"+temp1[0]+"_nnn1']").val(temp1[2]);
                $("input[name='projectitem"+temp1[0]+"_nnn2']").val(temp1[3]);
            }
        }
    }
    function insertContent(obj){
        if($("#ticheng").find("#projectitem"+obj).size()){
            $("#ticheng").find("#projectitem"+obj).remove();
        }else{
            var title=$("input[name^='staffcate'][value='"+obj+"']").attr("title");
            var temp='';
            temp+='<table id="projectitem'+obj+'" cid="'+obj+'"><tr><td class="title">'+title+'</td><td>业绩分配</td><td>';
            temp+='<label class="form-inline"><input type="radio" name="projectitem'+obj+'_t1" value="1" lay-ignore/> 比例 ';
            temp+='<input type="text" name="projectitem'+obj+'_n1" onclick="selecth1(this)" class="form-control"> % </label>';
            temp+='<label class="form-inline"><input type="radio" name="projectitem'+obj+'_t1" value="2" lay-ignore /> 固定 ';
            temp+='<input type="text" name="projectitem'+obj+'_n2" onclick="selecth1(this)" class="form-control" /> 元</label>';
            temp+='</td><td>提成种类</td><td>';
            temp+='<label class="form-inline">';
            temp+='<input type="radio" name="projectitem'+obj+'_t2" value="1" lay-ignore/>';
            temp+='比例，轮牌 <input type="text" onclick="selecth1(this)" name="projectitem'+obj+'_nn1" class="form-control"> % 指定 <input type="text" onclick="selecth1(this)" name="projectitem'+obj+'_nn2" class="form-control"> %</label>';
            temp+='<label class="form-inline">';
            temp+='<input type="radio" name="projectitem'+obj+'_t2" value="2" lay-ignore/>';
            temp+='固定，轮牌 <input type="text" onclick="selecth1(this)" name="projectitem'+obj+'_nnn1" class="form-control"> 元 指定 <input type="text" onclick="selecth1(this)" name="projectitem'+obj+'_nnn2" class="form-control"> 元</label></td></tr></table>';
            $("#ticheng").append(temp);
        }
    }
    function checkSubmit(){
        if($("#ticheng table").size()==0){
            layer.alert("请添加工种");
            return false;
        }
        var tempary1=[];
        var tempary2=[];
        $("#ticheng table").each(function(){
            var temp1="";
            var cid=$(this).attr("cid");
            if($("input[name='projectitem"+cid+"_t1']:checked").val()==1){
                temp1="1#"+$("input[name='projectitem"+cid+"_n1']").val();
            }else{
                temp1="2#"+$("input[name='projectitem"+cid+"_n2']").val();
            }
            tempary1.push(cid+"#"+temp1);
            var temp2="";
            if($("input[name='projectitem"+cid+"_t2']:checked").val()==1){
                temp2+="1#"+$("input[name='projectitem"+cid+"_nn1']").val()+"#"+$("input[name='projectitem"+cid+"_nn2']").val();
            }else{
                temp2+="2#"+$("input[name='projectitem"+cid+"_nnn1']").val()+"#"+$("input[name='projectitem"+cid+"_nnn2']").val();
            }
            tempary2.push(cid+"#"+temp2);
        });
        $("#commission").val(tempary2.join(","));
        $("#performance").val(tempary1.join(","));
        return true;
    }
    function selecth1(obj){
        $(obj).prev("input[type='radio']").click();
    }
</script>

{/if}
<script>
    layui.use(['layer', 'laypage', 'element','form'], function(){
        var layer = layui.layer
                ,laypage = layui.laypage
                ,element = layui.element
                ,form = layui.form;
        form.render();
    });
    function deleteContent(url){
        layer.confirm('确认删除此内容？删除后将不可恢复', {}, function(){
            location.href=url;
        });
    }
</script>